﻿using ClosedXML.Excel;
using MediatR;
using MES.Server.API.Application.Command.ProcessCategory;
using MES.Server.API.Dto.ProcessProceduresDTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ReturnValue;

namespace MES.Server.API.Controllers
{
    /// <summary>
    /// 工艺分类控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProcessCategoryController : ControllerBase
    {
        private readonly IMediator mediator;
        public ProcessCategoryController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 获取工艺分类
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<ProcessCategoryDTO>> GetProcessCategorys([FromQuery] GetProcessCategoryCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 删除工艺分类
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete]
        public Task<ApiResult<int>> DeleteProcessCategorys([FromQuery] DeleteProcessCategoryCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 新增工艺分类
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> AddProcessCategorys([FromBody] AddProcessCategoryCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 修改工艺分类
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public Task<ApiResult<int>> UpdateProcessCategorys(UpdateProcessCategoryCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
/// 导出工艺分类
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<IActionResult> ExportProcessCategorys()
{
    var command = new GetProcessCategoryCommand();
    var processCategories = await mediator.Send(command);
    
    using var workbook = new XLWorkbook();
    var worksheet = workbook.Worksheets.Add("工艺分类");
    
    // 设置表头
    worksheet.Cell(1, 1).Value = "编号";
    worksheet.Cell(1, 2).Value = "工序分类编号";
    worksheet.Cell(1, 3).Value = "工序分类名称";
    worksheet.Cell(1, 4).Value = "排序";
    worksheet.Cell(1, 5).Value = "状态";
    worksheet.Cell(1, 6).Value = "备注";
    
    // 填充数据
    var rowIndex = 2;
    foreach (var item in processCategories.TotalPage)
    {
        worksheet.Cell(rowIndex, 1).Value = item.Id;
        worksheet.Cell(rowIndex, 2).Value = item.CategoryCode;
        worksheet.Cell(rowIndex, 3).Value = item.CategoryName;
        worksheet.Cell(rowIndex, 4).Value = item.SortOrder;
        worksheet.Cell(rowIndex, 5).Value = item.IsActive;
        worksheet.Cell(rowIndex, 6).Value = item.Remarks;
        rowIndex++;
        }
    
    // 调整列宽
    worksheet.Columns().AdjustToContents();
    
    // 设置响应头
    var stream = new MemoryStream();
    workbook.SaveAs(stream);
    var content = stream.ToArray();
    
    return File(content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "工艺分类.xlsx");
}


    }
}
